package com.xcity.game.activity2.platform.qzone;

import java.util.Calendar;
import java.util.List;

import com.alibaba.fastjson.TypeReference;
import com.xcity.db.entity.Activity;
import com.xcity.db.entity.Player;
import com.xcity.game.account.AccountChannel;
import com.xcity.game.activity2.ActivityImpl;
import com.xcity.game.activity2.platform.IPacketHandler;
import com.xcity.game.activity2.platform.PlatformGift;
import com.xcity.game.activity2.platform.PlatformGiftDispatchService;
import com.xcity.game.common.Cause;
import com.xcity.game.common.IRegistry;
import com.xcity.game.common.MessageConst;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.event.GameEvents;
import com.xcity.pomelo.push.msg.PlatformGiftPush;
import com.xcity.pomelo.request.PlatformGiftInfoRequest;
import com.xcity.pomelo.request.PlatformGiftRequest;
import com.xcity.pomelo.response.HintResponse;
import com.xcity.pomelo.response.PlatformGiftInfoResponse;

import naga.x.App;
import naga.x.event.Event;
import naga.x.event.EventListener;
import naga.x.net.Session;
import naga.x.service.ServiceException;

/**
 * QQ玩吧礼包（登录礼包）
 * @author yang.li
 *
 */
public class QZoneGiftActivity extends ActivityImpl implements IPacketHandler/*, DayListener*/, EventListener {

	private IRegistry<AccountChannel, IPacketHandler> registry;
	private List<QZoneGift> gifts;
//	private QZoneGift current;
	
	public QZoneGiftActivity(Activity entity) {
		super(entity);
		this.registry = App.getApp().getServiceManager().get(PlatformGiftDispatchService.class);
	}

	@Override
	public String getId() {
		return getClass().getName();
	}

	@Override
	public void init() {
		gifts = entity.getDataObj(new TypeReference<List<QZoneGift>>() {});
	}

	@Override
	public void save() {
	}

	@Override
	public void startup() throws ServiceException {
//		dayChanged();
		registry.register(AccountChannel.QQ_WANBA, this);
		registry.register(AccountChannel.QQ, this);
//		TimeUpdater.getInstance().addDayListener(this);
//		App.getApp().getEventManager().register(this);
	}

	@Override
	public void shutdown() throws ServiceException {
		registry.unRegister(AccountChannel.QQ_WANBA);
		registry.unRegister(AccountChannel.QQ);
//		App.getApp().getEventManager().unRegister(this);
	}

	@Override
	public boolean update() {
		return true;
	}

	@Override
	public void setDirty() {
		init();
//		dayChanged();
	}
	
//	@Override
//	public boolean dayChanged() {
//		if (!isActive()) {
//			return false;
//		}
//		return true;
//	}
	
//	private void initCurrent() {
//		// 昨日晚9点~当日晚9点 可领取当日礼包
//		Calendar cal = Calendar.getInstance();
//		cal.add(Calendar.HOUR_OF_DAY, 3);
//		int day = cal.get(Calendar.DAY_OF_WEEK);
//		// 将day转换为礼包索引
//		day -= 2;
//		if (day == -1) {
//			day = 6;
//		}
//		current = gifts.get(day);
//	}
	
	@Override
	public int[] getEventTypes() {
		return new int[] { GameEvents.EVENT_PLAYER_LOADED };
	}

	@Override
	public void handleEvent(Event event) {
//		playerLoaded(event.getParameter(0));
	}

	public QZoneGift getCurrentGift() {
//		if (current == null) {
//			initCurrent();
//		}
//		return current;
		// 昨日晚9点~当日晚9点 可领取当日礼包
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.HOUR_OF_DAY, 3);
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 将day转换为礼包索引
		day -= 2;
		if (day == -1) {
			day = 6;
		}
		return gifts.get(day);
	}

	@Override
	public void requestGiftInfo(Session session, PlatformGiftInfoRequest req) {
		QZoneGift current = getCurrentGift();
		if (current == null) {
			session.send(new HintResponse(req.getSerial(), MessageConst.HAVE_NO_REWARD));
			return;
		}
		Player player = session.getClient();
		session.send(new PlatformGiftInfoResponse(req.getSerial(), current, getState(player, current)));
	}
	
	@Override
	public void requestGift(Session session, PlatformGiftRequest req) {
		QZoneGift current = getCurrentGift();
		if (current == null) {
			session.send(new HintResponse(req.getSerial(), MessageConst.HAVE_NO_REWARD));
			return;
		}
		Player player = session.getClient();
		if (req.id != current.id) { // 请求的礼包id与当前可领取的礼包id必须相同
//			session.send(new Response(req.getSerial()));
			session.send(new PlatformGiftInfoResponse(req.getSerial(), current, PlatformGift.STATE_CAN_NOT_RECEIVE));
			return;
		}
		byte state = getState(player, current);
//		if (state == PlatformGift.STATE_CAN_NOT_RECEIVE) {
//			session.send(new HintResponse(req.getSerial(), MessageConst.EXCHANGE_CAN_NOT_RECEIVE));
//			return;
//		}
//		if (state == PlatformGift.STATE_RECEIVED) {
//			session.send(new HintResponse(req.getSerial(), MessageConst.EXCHANGE_RECEIVED));
//			return;
//		}
		if (state != PlatformGift.STATE_CAN_RECEIVE) {
			session.send(new PlatformGiftInfoResponse(req.getSerial(), current, state));
			return;
		}
		receive(player, current);
//		session.send(new Response(req.getSerial()));
		session.send(new PlatformGiftInfoResponse(req.getSerial(), current, PlatformGift.STATE_RECEIVE_SUCC));
	}

//	private void playerLoaded(Player player) {
//	}
	
	private void receive(Player player, QZoneGift gift) {
		player.poolSet(getPoolKey(gift), gift.id);
		gift.content.receive(player, Cause.QZONE_GIFT, false);
		player.send(new PlatformGiftPush(gift, PlatformGift.STATE_RECEIVED));
	}
	
	private static byte getState(Player player, QZoneGift gift) {
		int received = player.getPool().getInt(getPoolKey(gift), PlatformGift.STATE_CAN_RECEIVE);
		if (received == gift.id) {
			return PlatformGift.STATE_RECEIVED;
		}
		return PlatformGift.STATE_CAN_RECEIVE;
	}
	
	private static String getPoolKey(PlatformGift gift) {
		return PlayerPool.PROPERTY_QZONE_GIFT_PREFIX + gift.type;
	}
	
}
